package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.UserVoXZY;
import com.kgc.scd.service.UserLoginRegService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: com.kgc.scd.controller
 * @author: HarryXzy
 * @create: 2022-06-10 14:14
 * @description: 用户登陆注册的controller
 **/
@RestController
@Api(tags = "01、用户登陆注册入口")
public class UserLoginRegController {

    @Autowired
    private UserLoginRegService userLoginRegService;

    /**
    * @author: HarryXzy
    * @date: 2022/6/10 16:09
    * @return: RequestResult<PageSupport<Anime>>
    * @description: 生成请求微信官方，获取code值的完整地址，用户通过扫描访问该地址，进行手动授权
    */
    @GetMapping(value = "/wxauthCodeUrl")
    @ApiOperation(value = "获取微信登陆二维码", notes = "生成请求微信官方，获取code值的完整地址，用户通过扫描访问该地址，进行手动授权")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 200, message = "请求成功")
    })
    public RequestResult<String> getWechatAuthCodeUrl(){
        // 接口返回一个完整的获取code请求地址，前端拿到地址生成二维码（手动模拟）
        RequestResult<String> wechatAuthCodeUrl = userLoginRegService.getWechatAuthCodeUrl();
        return wechatAuthCodeUrl;
    }



    /**
    * @author: HarryXzy 
    * @date: 2022/6/10 17:16
    * @return: com.kgc.scd.util.RequestResult<java.lang.String> 
    * @description: 微信扫码登陆成功绑定手机号码
    */
    @GetMapping(value = "/wxloginBindphone")
    @ApiOperation(value = "微信登陆绑定手机号码", notes = "微信扫码登陆成功绑定手机号码")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 502, message = "微信号已经绑定"),
            @ApiResponse(code = 503, message = "验证码失效"),
            @ApiResponse(code = 504, message = "验证码错误"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wxId", value = "微信ID"),
            @ApiImplicitParam(name = "phoneNO", value = "电话号"),
            @ApiImplicitParam(name = "phoneCode", value = "验证码")
    })
    public RequestResult<String> wxloginBindphone(
                                                  @RequestParam("wxId") String wxId,
                                                  @RequestParam("phoneNO") String phoneNO,
                                                  @RequestParam("phoneCode") String phoneCode){
        Map<String, String> result = userLoginRegService.wxloginBindphone(wxId,phoneNO,phoneCode);
        if(result.get("code").equals("200")){
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"),result.get("msg"));
    }



    /**
     * @author: HarryXzy
     * @date: 2022/6/10 17:16
     * @return: com.kgc.scd.util.RequestResult<java.lang.String>
     * @description: 获取手机验证码,每30秒获取一次
     */
    @GetMapping(value = "/getPhoneCode")
    @ApiOperation(value = "获取手机验证码", notes = "获取手机验证码,30秒内只能获取一次")
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 505, message = "已获取验证码，30秒内只能获取一次"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNO", value = "电话号")
    })
    public RequestResult<String> getPhoneCode(@RequestParam("phoneNO") String phoneNO){
        Map<String, String> result = userLoginRegService.getPhoneCode(phoneNO);
        if(result.get("code").equals("200")){
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"),result.get("msg"));
    }


    /**
     * @author: HarryXzy
     * @date: 2022/6/10 11:30
     * @return: java.util.Map<java.lang.String,java.lang.String>
     * @description: 使用手机号快捷登陆或注册
     */
    @ApiOperation(value = "手机号快捷登陆", notes = "使用手机号快捷登陆或注册")
    @ApiResponses({
            @ApiResponse(code = 503, message = "验证码失效"),
            @ApiResponse(code = 504, message = "验证码错误"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号"),
            @ApiImplicitParam(name = "phoneCode", value = "电话验证码")
    })
    @PostMapping("/loginOrRigByPhone")
    public RequestResult<String> loginOrRigByPhone(@RequestParam("phone") String phoneNo,
                                                 @RequestParam("phoneCode") String phoneCode){
        Map<String, String> result = userLoginRegService.loginOrRigByPhone(phoneNo,phoneCode);
        if(result.get("code").equals("200")){
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"),result.get("msg"));
    }


    /**
    * @author: HarryXzy
    * @date: 2022/6/10 21:26
    * @return: java.util.Map<java.lang.String,java.lang.String>
    * @description: 使用用户名或电话号进行密码登陆
    */
    @ApiOperation(value = "普通登陆", notes = "使用用户名或电话号进行密码登陆")
    @ApiResponses({
            @ApiResponse(code = 506, message = "账号或密码错误"),
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userNameOrPhone", value = "用户名或手机号"),
            @ApiImplicitParam(name = "userPwd", value = "密码")
    })
    @PostMapping("/loginByUserNameAndPwd")
    public RequestResult<String> loginByUserNameAndPwd(@RequestParam("userNameOrPhone")String userNameOrPhone,
                                                     @RequestParam("userPwd")String userPwd) throws Exception {
        Map<String, String> result = userLoginRegService.loginByUserNameAndPwd(userNameOrPhone, userPwd);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"), result.get("msg"));
    }

    /**
     * @author: HarryXzy
     * @date: 2022/6/10 13:26
     * @return:
     * @description: 普通用户名密码注册
     */
    @ApiOperation(value = "用户名密码注册", notes = "使用用户名和密码进行注册")
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 503, message = "验证码失效"),
            @ApiResponse(code = 504, message = "验证码错误"),
            @ApiResponse(code = 507, message = "用户名已重复"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneCode", value = "手机验证码")
    })
    @PostMapping("/userRegister")
    public  RequestResult<String> userRegister(@RequestBody UserVoXZY user,
                                            @RequestParam("phoneCode")String phoneCode){
        Map<String, String> result = userLoginRegService.userRegister(user, phoneCode);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"), result.get("msg"));
    }


    /**
     * @author: HarryXzy
     * @date: 2022/6/10 13:26
     * @return:
     * @description: 用户退出登陆
     */
    @ApiOperation(value = "用户退出登陆", notes = "用户退出登陆")
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 200, message = "绑定登陆成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌")
    })
    @PostMapping("/userLogOut")
    @RequestPermission
    public  RequestResult<String> userLogOut(@RequestHeader String token){
        System.out.println(token);
        Map<String, String> result = userLoginRegService.userLogOut(token);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code"), result.get("msg"));
    }



}
